package com.ruoyi.baseMain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.dto.RoleDTO;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.baseMain.domain.bo.BasePolicyBo;
import com.ruoyi.baseMain.domain.vo.BasePolicyVo;
import com.ruoyi.baseMain.domain.BasePolicy;
import com.ruoyi.baseMain.mapper.BasePolicyMapper;
import com.ruoyi.baseMain.service.IBasePolicyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 客户政策Service业务层处理
 *
 * @author linziheng
 * @date 2024-01-08
 */
@RequiredArgsConstructor
@Service
public class BasePolicyServiceImpl implements IBasePolicyService {

    private final BasePolicyMapper baseMapper;
    private final SysUserMapper sysUserMapper;
    private final SysDeptMapper sysDeptMapper;
    /**
     * 查询客户政策
     */
    @Override
    public BasePolicyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户政策列表
     */
    @Override
    public TableDataInfo<BasePolicyVo> queryPageList(BasePolicyBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        String user = "";
        for (RoleDTO role : loginUser.getRoles()) {
            if(1719534201567039490L == role.getRoleId() || 1L== role.getRoleId() || 1720783927643512833L == role.getRoleId()
                || 1720783977803194369L == role.getRoleId()|| 1741717746987237377L == role.getRoleId()|| 1750333840374910977L == role.getRoleId()){
                user = "admin";
                break;
            }
        }
        if(!"admin".equals(user)){
            bo.setOperateUserName(loginUser.getNickName());
        }
        SysUser uuu = sysUserMapper.selectUserById(loginUser.getUserId());
        SysDept sysDept = new SysDept();
        if(!"admin".equals(user)){
            sysDept = sysDeptMapper.findDeptByLeader(uuu.getRemark());
            if(!Objects.isNull(sysDept)){
                //根据id 查询部门名称
                bo.setBusinessUnitName(sysDept.getDeptName());
                bo.setOperateUserName(null);
            }else{
                bo.setOperateUserName(uuu.getNickName());
            }
            //业务中泰看所有
            if(uuu.getDeptId()==1811377323661430787L){
                bo.setBusinessUnitName(null);
                bo.setOperateUserName(null);
            }
        }


        LambdaQueryWrapper<BasePolicy> lqw = buildQueryWrapper(bo);
        Page<BasePolicyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户政策列表
     */
    @Override
    public List<BasePolicyVo> queryList(BasePolicyBo bo) {
        LambdaQueryWrapper<BasePolicy> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BasePolicy> buildQueryWrapper(BasePolicyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BasePolicy> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), BasePolicy::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getSignatory()), BasePolicy::getSignatory, bo.getSignatory());
        lqw.eq(StringUtils.isNotBlank(bo.getPolicyType()), BasePolicy::getPolicyType, bo.getPolicyType());
        lqw.eq(StringUtils.isNotBlank(bo.getPolicyTag()), BasePolicy::getPolicyTag, bo.getPolicyTag());
        lqw.eq(StringUtils.isNotBlank(bo.getPayType()), BasePolicy::getPayType, bo.getPayType());
        lqw.eq(StringUtils.isNotBlank(bo.getCooperateType()), BasePolicy::getCooperateType, bo.getCooperateType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BasePolicy::getStatus, bo.getStatus());
        lqw.eq(bo.getAccountPoint() != null, BasePolicy::getAccountPoint, bo.getAccountPoint());
        lqw.eq(bo.getPrivatePoint() != null, BasePolicy::getPrivatePoint, bo.getPrivatePoint());
        lqw.eq(bo.getTotalPoint() != null, BasePolicy::getTotalPoint, bo.getTotalPoint());
        lqw.eq(bo.getMainParty() != null, BasePolicy::getMainParty, bo.getMainParty());
        lqw.eq(StringUtils.isNotBlank(bo.getOperateUser()), BasePolicy::getOperateUser, bo.getOperateUser());
        lqw.like(StringUtils.isNotBlank(bo.getOperateUserName()), BasePolicy::getOperateUserName, bo.getOperateUserName());
        lqw.like(StringUtils.isNotBlank(bo.getBusinessUnitName()), BasePolicy::getBusinessUnitName, bo.getBusinessUnitName());
        lqw.orderByDesc(BasePolicy::getTakeTime);
        lqw.orderByDesc(BasePolicy::getCreateTime);
        return lqw;
    }

    /**
     * 新增客户政策
     */
    @Override
    public Boolean insertByBo(BasePolicyBo bo) {
        BasePolicy add = BeanUtil.toBean(bo, BasePolicy.class);

        try {
            LoginUser loginUser = LoginHelper.getLoginUser();
            if(!Objects.isNull(loginUser)){
                add.setOperateUserName(loginUser.getNickName());
                add.setBusinessUnitName(loginUser.getDeptName());
            }
        }catch(Exception e) {

        }
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户政策
     */
    @Override
    public Boolean updateByBo(BasePolicyBo bo) {
        BasePolicy update = BeanUtil.toBean(bo, BasePolicy.class);
        try {
            LoginUser loginUser = LoginHelper.getLoginUser();
            if(!Objects.isNull(loginUser)){
                update.setOperateUserName(loginUser.getNickName());
                update.setBusinessUnitName(loginUser.getDeptName());
            }
        }catch(Exception e) {

        }


        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BasePolicy entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户政策
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public List<BasePolicyBo> queryListInstanceIdIsNotNull() {
        return baseMapper.queryListInstanceIdIsNotNull();
    }
}
